package com.zhangtai.modules.dao;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dto.ProtocolDto;
import com.zhangtai.modules.entity.contract.ProtocolPersonnelEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 培训协议人员列表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-10-21 16:30:00
 */
@Mapper
public interface ProtocolPersonnelDao extends BaseMapper<ProtocolPersonnelEntity> {

    @Select(
            "<script>SELECT\n" +
                    "\ttpp.*,\n" +
                    "\ttp.*,\n" +
                    "\ttei.name,\n" +
                    "\ttei.code,\n" +
                    "\ttei.idcard,\n" +
                    "\ttei.gender,\n" +
                    "\ttei.employeestatus\n" +
                    "FROM\n" +
                    "\ttb_protocol_personnel tpp\n" +
                    "\tLEFT JOIN tb_employee_info tei ON tei.oid = tpp.oid\n" +
                    "\tLEFT JOIN tb_protocol tp ON tp.id = tpp.protocol_id " +
                    " <where> tpp.is_delete = 0 " +
                    "<if test='orgIds!=null and orgIds.size>0'> and tpp.unit_id in " +
                    " <foreach item=\"orgId\" index=\"index\" collection=\"orgIds\" open=\"(\" separator=\",\" close=\")\">#{orgId}</foreach>" +
                    "</if>"+
                    "<if test='orgIds==null or orgIds.size==0'> and tpp.unit_id = null </if>" +

                    "<if test='name!=null'> and tei.name like '%${name}%' </if>" +
                    "<if test='code!=null'> and tei.code like '%${code}%' </if>" +
                    "<if test='projectName!=null'> and tp.project_name like '%${projectName}%' </if>" +
                    "   and (tpp.status = 2 or tpp.status = 3 or tpp.status = 4) "+
                    "</where>  ORDER BY tpp.create_time DESC " +
                    "</script>"
    )
    List<ProtocolDto> getProtocolPersonnelList(
            @Param("orgIds") List<Long> orgIds,
            @Param(value = "name") String name,
            @Param(value = "code") String code,
            @Param(value = "projectName") String projectName,
            Page<ProtocolDto> page
    );

    @Select(
            "<script>SELECT\n" +
                    "\ttpp.*,\n" +
                    "\ttp.*,\n" +
                    "\ttei.name,\n" +
                    "\ttei.code,\n" +
                    "\ttei.idcard,\n" +
                    "\ttei.gender\n" +
                    "FROM\n" +
                    "\ttb_protocol_personnel tpp\n" +
                    "\tLEFT JOIN tb_employee_info tei ON tei.oid = tpp.oid\n" +
                    "\tLEFT JOIN tb_protocol tp ON tp.id = tpp.protocol_id " +
                    " <where> tpp.is_delete = 0 " +
                    "<if test='oid!=null'> and tpp.oid = #{oid} </if>" +

                    "<if test='projectName!=null'> and tp.project_name like '%${projectName}%' </if>" +
                    "</where>  ORDER BY tpp.create_time DESC " +
                    "</script>"
    )
    List<ProtocolDto> getProtocolPersonnelListByOid(
            @Param( value = "oid") Long oid,
            @Param(value = "projectName") String projectName,
            Page<ProtocolDto> page
    );

    @Select(
            "<script>SELECT\n" +
                    "\ttpp.*,\n" +
                    "\ttp.*,\n" +
                    "\ttei.name,\n" +
                    "\ttei.code,\n" +
                    "\ttei.idcard,\n" +
                    "\ttei.gender,\n" +
                    "\ttei.employeestatus\n" +
                    "FROM\n" +
                    "\ttb_protocol_personnel tpp\n" +
                    "\tLEFT JOIN tb_employee_info tei ON tei.oid = tpp.oid\n" +
                    "\tLEFT JOIN tb_protocol tp ON tp.id = tpp.protocol_id " +
                    " <where> tpp.is_delete = 0 " +
                    "<if test='protocolId!=null'> and tpp.protocol_id = #{protocolId} </if>" +

                    "</where>  ORDER BY tpp.create_time DESC " +
                    "</script>"
    )
    List<ProtocolDto> getProtocolPersonnelListByProtocolId(@Param("protocolId") Long protocolId );



    @Select(
            "<script>  SELECT  *  FROM\n" +
                    "\ttb_protocol_personnel tpp\n" +
                    "\tLEFT JOIN tb_protocol tp ON tp.id = tpp.protocol_id " +
                    " <where> tpp.is_delete = 0 " +
                    "<if test='oid!=null'> and tpp.oid = #{oid} </if>" +
                    "</where>  ORDER BY tpp.create_time DESC " +
                    "</script>"
    )
    List<ProtocolDto> getProtocolPersonnelByOid(
            @Param( value = "oid") String oid,
            Page<ProtocolDto> page
    );

}
